<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { getPatientListApi, removePatientApi } from '@/apis/patient'
  import { useConsultStore } from '@/stores/consult'
  import type { PatientItem } from '@/types/patient'
  import { onShow } from '@dcloudio/uni-app'
  import { ref } from 'vue'

  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])

  // 1. 获取患者列表
  const patientList = ref<PatientItem[]>([])
  const getPatientList = async () => {
    const res = await getPatientListApi()
    patientList.value = res

    // 1.1 判断：如果是选择患者
    if (props.type === 'consult' && store.patientId === '') {
      // 如果有默认患者，就会默认选中
      patientList.value.forEach((item) => {
        if (item.defaultFlag === 1) {
          store.patientId = item.id!
        }
      })
    }
  }
  onShow(() => {
    getPatientList()
  })

  // 2. 删除患者
  const onSwipeActionClick = async (id: string, index: number) => {
    // 2.1 调用删除接口
    await removePatientApi(id)
    // 2.2 从数组中移除患者对象
    // patientList.value = patientList.value.filter(item => item.id !== id)
    patientList.value.splice(index, 1)

    // 2.3 删除判断
    if (props.type === 'consult') {
      if (store.patientId === id) store.patientId = ''
    }
  }

  // 3. 处理选择患者
  const props = defineProps<{
    type: 'consult' | 'archive'
  }>()
  if (props.type === 'consult') {
    // 选择患者
    uni.setNavigationBarTitle({ title: '选择患者' })
  }
  const store = useConsultStore()
  const selectPatient = (id: string) => {
    if (props.type !== 'consult') return
    store.patientId = id
  }

  // 4. 跳转到下一页
  const nextSetp = () => {
    if (!store.patientId) return uni.showToast({ title: '请选择患者' })
    uni.navigateTo({
      url: '/subpkg_consult/payment/index',
    })
  }
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view v-if="type === 'consult'">
        <view>请选择患者信息</view>
        <view>以便医生给出更准确的治疗，信息仅医生可见</view>
      </view>
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="(item, index) in patientList"
          :key="item.id"
          :right-options="swipeOptions"
          @click="onSwipeActionClick(item.id!, index)"
        >
          <!-- :class="{ active: item.defaultFlag === 1 }" -->
          <view
            class="archive-card"
            :class="{ active: store.patientId === item.id && type === 'consult' }"
            @click="selectPatient(item.id!)"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard!.replace(/^(.{6}).+(.{4})$/, '$1********$2')
              }}</text>
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/index">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>

      <!-- 跳转到下一个页面 -->
      <view v-if="type === 'consult'">
        <button class="uni-button" @click="nextSetp">下一步</button>
      </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './index.scss';
</style>
